package com.briup.ch04;

import java.util.Arrays;

public class Test6 {
    public static void main(String[] args) {
        int[] arr={54,32,16,76,88,67,55,37};
        //二分查找的前提是对有序数组查询
        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));
        int numOfIndex=binerySearch(arr,88);
        if(numOfIndex!=-1)
            System.out.println(numOfIndex);
        else
            System.out.println("找不到");
        int[] arr2=Arrays.copyOf(arr,20);

    }
    public static int binerySearch(int[] arr,int num)
    {
        int start=0;
        int end=arr.length-1;
        int mid=0;
        while(true)
        {
            mid=(start+end)/2;
            if(arr[mid]==num)
            {
                return mid;
            }
            else if(arr[mid]<num)
            {
                start=mid+1;
            }
            else
            {
                end=mid-1;
            }
            if(start>end)
            {
               break;
            }
        }
        return -1;
    }

}
